package com.laijiale.controller;

import com.laijiale.constant.ML;
import com.laijiale.dto.*;
import com.laijiale.entity.User;
import com.laijiale.result.Result;
import com.laijiale.service.UserService;
import com.laijiale.until.EasyExcelUtil;
import com.laijiale.vo.LoginVO;
import com.laijiale.vo.UserSimpleListVO;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 *  控制层。
 *
 * @author laijiale
 * @since 2024-12-22
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Operation(summary ="新增－ 单条",description ="新增一条用户记录")
    @PostMapping("insert")
    public boolean insert(@Validated @RequestBody UserInsertDTO dto) {
        return userService.insert(dto);
    }
    @Operation(summary="查询－ 单条",description="按主键查询一条用户记录")
    @GetMapping("select/{id}")
    public User select(@PathVariable("id") Long id) {
        return userService.select(id);
    }
    @Operation(summary ="查询－ 简单全查",description="查询全部用户记录")
    @GetMapping("simpleList")
    public List<UserSimpleListVO> simpleList() {
        return userService.simpleList();
    }
    @Operation(summary="查询－ 分页",description ="分页查询用户记录")
    @GetMapping("page")
    public Page<User> page(@Validated UserPageDTO dto) {
        return userService.page(dto);
    }

    @Operation(summary="修改－单条",description ="按主键修改一条用户记录")
    @PutMapping("update")
    public boolean update(@RequestBody UserUpdateDTO dto) {
        return userService.update(dto);
    }
    @Operation(summary="删除－ 单条",description="按主键删除一条用户记录")
    @DeleteMapping("delete/{id}")
    public boolean delete(@PathVariable("id") Long id) {
        return userService.delete(id);
    }
    @Operation(summary="删除－ 批量",description ="按主键批量删除用户记录")
    @DeleteMapping("deleteBatch")
    public boolean deleteBatch(@RequestParam("ids") List<Long> ids) {
        return userService.deleteBatch(ids);
    }
    @Operation(summary="查询－ 角色列表",description ="按主键查询用户的全部角色id")
    @GetMapping("listRoleIds/{id}")
    public List<Long> listRoleIds(@PathVariable("id") Long id) {
        return userService.listRoleIds(id);
    }
    @Operation(summary="修改－ 角色列表",description ="按主键修改用户的全部角色id")
    @GetMapping("updateRoles")
    public boolean updateRoles(@RequestBody UserUpdateDTO dto) {
        return userService.updateRoles(dto);
    }
    @Operation(summary="修改－重置密码",description ="按主键重置用户密码")
    @PutMapping("resetPassword/{id}")
    public boolean resetPassword(@PathVariable("id")  Long id) {
        return userService.resetPassword(id);
    }
    @Operation(summary="修改－改密码",description ="修改登录密码")
    @PutMapping("updatePassword")
    public boolean updatePassword(@RequestBody UserUpdatePasswordDTO dto) {
        return userService.updatePassword(dto);
    }
    @Operation(summary="查询－ 统计数据",description ="查询用户相关的统计数据")
    @GetMapping("statistics")
    public Map<String, Object> statistics() {
        return userService.statistics();

    }
    @Operation(summary="查询－ 报表打印",description ="打印用户相关的报表数据")
    @GetMapping("excel")
    public void excel(HttpServletResponse response) {
        EasyExcelUtil.download(response,
                ML.User.EXCEL_FILE_NAME,
                ML.User.EXCEL_SHEET_NAME,
                UserExcelDTO.class,
                userService.excel());
    }

    @Operation(summary="查询 - 验证码",description ="根据手机号码生成一个随机6位短信验证码")
    @GetMapping("getVcode/{phone}")
    public Result<String> getVcode(@PathVariable String phone){
        return new Result<>(userService.getVcode(phone));
    }

    @Operation(summary = "查询--验证码登录",description = "按照用户的手机号验证码登录")
    @PutMapping("loginByPhone")
    public LoginVO loginByPhone(@RequestBody LoginByPhoneDTO dto){
        return userService.loginByPhone(dto);
    }
    @Operation(summary="查询 - 用户登录",description ="根据账号密码登录并返回指定信息")
    @PostMapping("login")
    public Result<LoginVO> login(@RequestBody @Validated LoginDTO dto){
        return new Result<>(userService.login(dto));
    }
    @Operation(summary="修改 - 修改用户头像",description ="修改用户头像")
    @PostMapping("uploadAvatar/{id}")
    public Result<String> uploadAvatar(MultipartFile avatarFile, @PathVariable("id") Long id){
        return userService.uploadAvatar(avatarFile, id);
    }

    }

